import { sha256 } from "ethers/lib/utils";

const cryptico = require("cryptico");

// The length of the RSA key, in bits.
const Bits = 1024;
const ownerPhrase = "0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C18080"; //管理者的私钥用钱包地址+8080
//const ownerPhrase="0x72FaddEA47aCd619b820cFfe5f61EEFA52028d878080"
const ownerPub =
  "cHhzNdmF0iqU6qqd62EeQAlSYaYz3xOCf8eXqMTKB4yzIHe0sKnUMpuoow4GG6BffQGrwZFbRtVpGh9a81v770o6dB6kfFckQ8HLnFCsMNpUgBcZZauw7yLh00aRCD2CfKj1LuCE2A9FS4fHcPiqRWmgEq77Or/meNrkVhT25L8=";
export function getKeyPair(inPhrase) {
  console.log(inPhrase);
  let PassPhrase = sha256(inPhrase);
  console.log(PassPhrase);
  let privateKey = cryptico.generateRSAKey(PassPhrase, Bits);
  let publicKey = cryptico.publicKeyString(privateKey);
  return { PassPhrase, publicKey };
}

export function encrypt(PlainText, publicKey) {
  let EncryptionResult = cryptico.encrypt(PlainText, publicKey);
  return EncryptionResult.cipher;
}

export function decrypt(cipher, PassPhrase) {
  console.log(PassPhrase);
  let privateKey = cryptico.generateRSAKey(PassPhrase, Bits);
  let DecryptionResult = cryptico.decrypt(cipher, privateKey);
  console.log("pri=", DecryptionResult);
  return DecryptionResult.plaintext;
}
